

const sealedbox = require('tweetnacl-sealedbox-js');
const naclUtil = require('tweetnacl-util');

function encPassword() {

    var h, i = globalThis.crypto,
        j = 64,
        k = 1,
        l = 1,
        m = 1,
        n = 2,
        o = 32,
        p = 16,
        q = l + m + n + o + sealedbox.overheadLength + p;
    function r(a, b) {
        return sealedbox.seal(a, b)
    }
    function s(a) {
        var b = [];
        for (var c = 0; c < a.length; c += 2)
            b.push(parseInt(a.slice(c, c + 2), 16));
        return new Uint8Array(b)
    }
    function a(a, d, e, f) {
        var g = q + e.length;
        if (d.length != j)
            throw new Error("public key is not a valid hex sting");
        var t = s(d);
        if (!t)
            throw new Error("public key is not a valid hex string");
        var u = new Uint8Array(g)
            , v = 0;
        u[v] = k;
        v += l;
        u[v] = a;
        v += m;
        d = {
            name: "AES-GCM",
            length: o * 8
        };
        var w = {
            name: "AES-GCM",
            iv: new Uint8Array(12),
            additionalData: f,
            tagLen: p
        };
        return i.subtle.generateKey(d, !0, ["encrypt", "decrypt"]).then(function (a) {
            var c = i.subtle.exportKey("raw", a);
            a = i.subtle.encrypt(w, a, e.buffer);
            return Promise.all([c, a])
        }).then(function (a) {
            var b = new Uint8Array(a[0]);
            b = r(b, t);
            u[v] = b.length & 255;
            u[v + 1] = b.length >> 8 & 255;
            v += n;
            u.set(b, v);
            v += o;
            v += sealedbox.overheadLength;
            if (b.length !== o + sealedbox.overheadLength)
                throw new Error("encrypted key is the wrong length");
            b = new Uint8Array(a[1]);
            a = b.slice(-p);
            b = b.slice(0, -p);
            u.set(a, v);
            v += p;
            u.set(b, v);
            return u
        })
    }

    const as = "181"
    const bs = "89c61e2da1ee39a9c587e4d42cc96be33071fa40cd5e3a2d595efeecbebc0061"
    const es = naclUtil.decodeUTF8("13512345678");
    const randomTime = Math.trunc(Date.now() / 1000).toString()
    const ks = naclUtil.decodeUTF8(randomTime);

    const ps = a(as, bs, es, ks).then((res) => {
        let j = '#PWD_INSTAGRAM_BROWSER'
        let g = 10 //版本号
        let f = randomTime
        return [j, g, f, naclUtil.encodeBase64(res)].join(":")
    })
    return ps
}